package com.fhx.core.utils

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.graphics.Color
import android.os.Build
import android.util.TypedValue
import android.view.View
import android.view.ViewGroup
import com.fhx.core.base.BaseActivity

object ScreenUtil {

    @JvmStatic
    fun getScreenWidth(context: Context) = context.resources.displayMetrics.widthPixels

    @JvmStatic
    fun getScreenHeight(context: Context) = context.resources.displayMetrics.heightPixels

    @JvmStatic
    fun getScreenDensity(context: Context) = context.resources.displayMetrics.density

    @JvmStatic
    fun dip2px(context: Context, dpValue: Float) = dpValue * getScreenDensity(context) + 0.5f

    @JvmStatic
    fun sp2px(context: Context, spValue: Float) =
        TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, spValue, context.resources.displayMetrics)

    @JvmStatic
    fun px2dip(context: Context, pxValue: Float) = pxValue / getScreenDensity(context) + 0.5f

    @JvmStatic
    fun px2sp(context: Context, pxValue: Float) = (pxValue / context.resources.displayMetrics.scaledDensity)

    @JvmStatic
    fun getDpixel(context: Context, value: Int) = getScreenDensity(context) * value

    @JvmStatic
    fun getStatusBarHeight(context: Context): Int {
        val resourceRes = context.resources.getIdentifier("status_bar_height", "dimen", "android")
        return if (resourceRes > 0) context.resources.getDimensionPixelSize(resourceRes) else 0
    }

    @JvmStatic
    fun setStatusBarColor(context: Context, color: String) {
        val statusBarView = View(context)
        val lp = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, getStatusBarHeight(context))
        statusBarView.setBackgroundColor(Color.parseColor(color))
        val contentView = (context as Activity).findViewById<ViewGroup>(android.R.id.content)
        contentView.addView(statusBarView, lp)
    }

    @JvmStatic
    fun getActionBarSize(context: Context): Int {
        val tv = TypedValue()
        return if (context.theme.resolveAttribute(android.R.attr.actionBarSize, tv, true))
            TypedValue.complexToDimensionPixelSize(tv.data, context.resources.displayMetrics)
        else 0
    }


    fun setStatusBarMode(activity:BaseActivity,isDark: Boolean) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            activity.window.decorView.systemUiVisibility =
                if (isDark) {
                    View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or
                            View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
                } else {
                    View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                }

        } else {
            when(Build.BRAND){
                "Xiaomi"-> setMIUIMode(activity, isDark)
                "Meizu"-> setFlymeMode(activity, isDark)
                else-> {
                    activity.window.decorView.systemUiVisibility = if (isDark){
                        View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    }else{
                        View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    }

                }

            }

        }
    }
    @SuppressLint("PrivateApi")
    private fun setMIUIMode(activity: BaseActivity,isDark: Boolean){
        val clazz = activity.window.javaClass
        try {
            var darkModeFlag = 0
            val layoutParams = Class.forName("android.view.MiuiWindowManager\$LayoutParams")
            val field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE")
            darkModeFlag = field.getInt(layoutParams)
            val extraFlagField =
                clazz.getMethod("setExtraFlags", Int::class.javaPrimitiveType, Int::class.javaPrimitiveType)
            extraFlagField.invoke(activity.window, if (isDark) darkModeFlag else 0, darkModeFlag)
        } catch (e: Exception) {
            e.printStackTrace()
        }

    }
    private fun setFlymeMode(activity: BaseActivity,isDark: Boolean){
        try {
            val attributes = activity.window.attributes
            val f = attributes.javaClass.getDeclaredField("MEIZU_FLAG_DARK_STATUS_BAR_ICON")
            f.isAccessible = true
            val bits = f.getInt(attributes)
            val f2 = attributes.javaClass.getDeclaredField("meizuFlags")
            f2.isAccessible = true
            var meizuFlags = f2.getInt(attributes)
            val oldFlags = meizuFlags
            meizuFlags = if (isDark) {
                meizuFlags or bits
            } else {
                meizuFlags and bits.inv()
            }
            if (oldFlags != meizuFlags) {
                f2.setInt(attributes, meizuFlags)
            }
        } catch (e: NoSuchFieldException) {
            e.printStackTrace()
        } catch (e: IllegalAccessException) {
            e.printStackTrace()
        } catch (e: IllegalArgumentException) {
            e.printStackTrace()
        } catch (e: Throwable) {
            e.printStackTrace()
        }

    }

}